package com.hnhcyy.goods.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hnhcyy.b2b.common.exception.ExceptionCast;
import com.hnhcyy.goods.mapper.GoodsExtendMapper;
import com.hnhcyy.goods.mapper.SpMchkMapper;
import com.hnhcyy.goods.mapper.SpOldMchkMapper;
import com.hnhcyy.goods.pojo.GoodsExtend;
import com.hnhcyy.goods.pojo.GoodsResultEnum;
import com.hnhcyy.goods.pojo.SpMchk;
import com.hnhcyy.goods.pojo.SpOldMchk;
import com.hnhcyy.goods.service.GoodsRangeService;

@Service
public class GoodsRangeServiceImpl implements GoodsRangeService{

	@Autowired
	private SpOldMchkMapper spOldMchkMapper;
	@Autowired
	private SpMchkMapper spMchkMapper;
	@Autowired
	private GoodsExtendMapper goodsExtendMapper;

	@Transactional
	@Override
	public boolean oldCustRange(List<SpOldMchk> list) {
		String dwbh = list.get(0).getDwbh();
		// 查询该客户是否有记录
		int count = spOldMchkMapper.selectCountByDwbh(dwbh);
		if(count > 0) {
			// 删除该客户的所有商品
			int rows = spOldMchkMapper.deleteItemByDwbh(dwbh);
			if(rows != count) {
				ExceptionCast.cast(GoodsResultEnum.SPOLDMCHK_DELETE_FAIL);
			}
		}
		int affectRows = spOldMchkMapper.insertBatchItem(list);
		return affectRows >= list.size();
	}

	@Override
	public boolean custAdjustRange(List<SpMchk> list) {
		String dwbh = list.get(0).getDwbh();
		// 查询该客户是否有记录
		int count = spMchkMapper.selectCountByDwbh(dwbh);
		if(count > 0) {
			// 删除该客户的所有商品
			int rows = spMchkMapper.deleteItemByDwbh(dwbh);
			if(rows != count) {
				ExceptionCast.cast(GoodsResultEnum.SPMCHK_DELETE_FAIL);
			}
		}
		int affectRows = spMchkMapper.insertBatchItem(list);
		return affectRows >= list.size();
	}

	@Override
	public boolean provinceRange(GoodsExtend goodsExtend) {
		int count = goodsExtendMapper.selectCountBySjidAndSpid(goodsExtend.getSjid(), goodsExtend.getSpid());
		if(count > 0) {
			return goodsExtendMapper.updateItem(goodsExtend) > 0;
		}else {
			return goodsExtendMapper.insertItem(goodsExtend) > 0;
		}
	}

}
